package service

import (
	"fmt"
	"github.com/xuri/excelize/v2"
	"go.uber.org/zap"
	"strconv"
	"svs/global"
	"svs/model"
	"svs/model/response"
	"svs/util"
	"time"
)

func GetLogList(title string, startAt string, endAt string, pageInfo util.PageInfo) (model.Page, error) {
	var logList []model.Log
	var total int64
	zap.L().Debug("log", zap.Any("pageinfo", pageInfo))
	query := global.DB
	query = global.DB.Model(&model.Log{}).Where("status=?", 200).Where("api_name != ?", ``).Where(query.Where("api_name like ?", "%"+title+"%").Or("ip like ? ", "%"+title+"%"))

	if startAt != "" {
		startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", startAt, time.Local)
		query.Where("create_time>=?", startTime.Unix())
	}
	if endAt != "" {
		endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", endAt, time.Local)
		query.Where("create_time<=?", endTime.Unix())
	}

	err := query.Count(&total).Error
	err = query.Preload("User").Limit(pageInfo.Size).Order("create_time desc").Offset(pageInfo.Offset).Find(&logList).Statement.Error
	var Logs []response.LogList
	for _, log := range logList {
		logi := response.LogList{
			Id: log.Id,
		}
		Logs = append(Logs, logi)
	}
	return model.Page{
		Data:  Logs,
		Total: total,
	}, err
}

func ExportLog(title string, startAt string, endAt string, fileName string) {
	var logList []model.Log
	//数据，所有的符合要求的数据
	query := global.DB
	query = global.DB.Model(&model.Log{}).Where("status=?", 200).Where("api_name != ?", ``).Where(query.Where("api_name like ?", "%"+title+"%").Or("ip like ? ", "%"+title+"%"))

	if startAt != "" {
		startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", startAt, time.Local)
		query.Where("create_time>=?", startTime.Unix())
	}
	if endAt != "" {
		endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", endAt, time.Local)
		query.Where("create_time<=?", endTime.Unix())
	}
	query.Preload("User").Order("create_time desc").Find(&logList)
	f := excelize.NewFile()
	// Create a new sheet.
	f.SetColWidth("Sheet1", "A", "A", 10)
	f.SetColWidth("Sheet1", "B", "B", 12)
	f.SetColWidth("Sheet1", "C", "D", 40)
	f.SetCellValue("Sheet1", "A1", "序号")
	f.SetCellValue("Sheet1", "B1", "ip地址")
	f.SetCellValue("Sheet1", "C1", "操作角色")
	f.SetCellValue("Sheet1", "D1", "操作内容")
	f.SetCellValue("Sheet1", "E1", "操作时间")
	for i, log := range logList {
		f.SetCellValue("Sheet1", "A"+strconv.Itoa(i+2), i+1)
		f.SetCellValue("Sheet1", "B"+strconv.Itoa(i+2), log.Ip)
		f.SetCellValue("Sheet1", "C"+strconv.Itoa(i+2), log.Role)
		f.SetCellValue("Sheet1", "D"+strconv.Itoa(i+2), log.ApiName)
		f.SetCellValue("Sheet1", "E"+strconv.Itoa(i+2), time.Unix(log.CreateTime, 0).Format("2006-01-02 15:04:05"))
	}
	// Set active sheet of the workbook.
	f.SetActiveSheet(1)
	f.SetDocProps(&excelize.DocProperties{
		Category: "log",
		Creator:  "zfInfo",
	})
	// Save spreadsheet by the given path.
	if err := f.SaveAs("./static/log/" + fileName + ".xlsx"); err != nil {
		fmt.Println(err)
	}
}

type LogObj struct {
}

// 根据请求地址和方法获取中文名
func GetApiName(path, method, body string) string {
	var logO LogObj
	switch method + ":" + path {
	case "POST:/api/user/create": //添加管理员
		return logO.LogUser(body)
	}
	return ""
}
func (o LogObj) LogUser(body string) string {
	return ""
}
